package com.shiwaixiangcun.customer.pay

import android.content.Context
import android.util.Log

import com.tencent.mm.opensdk.constants.Build
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory

/**
 *
 * @author Administrator
 * @date 2017/9/22
 */

class WeiXinPay(private val mWeiXinResponseBean: WeiXinInfo.WeiXinResponseBean) {

    private val Bug = "WeiXinPay"
    private var api: IWXAPI? = null

    val isPaySupported: Boolean
        get() = api != null && api!!.wxAppSupportAPI >= Build.PAY_SUPPORTED_SDK_INT

    /**
     * 注册APPID
     *
     * @param mContext
     * @return
     */
    fun createWXAPI(mContext: Context) {
        Log.e("微信支付", mWeiXinResponseBean.appid)
        api = WXAPIFactory.createWXAPI(mContext, mWeiXinResponseBean.appid, true)
        api!!.registerApp(mWeiXinResponseBean.appid)
    }

    /**
     * 调用支付
     *
     * @param
     */
    fun sendPayReq() {
        val request = PayReq()
        request.appId = mWeiXinResponseBean.appid
        request.partnerId = mWeiXinResponseBean.partnerid
        request.prepayId = mWeiXinResponseBean.prepayid
        request.nonceStr = mWeiXinResponseBean.noncestr
        request.timeStamp = mWeiXinResponseBean.timestamp
        request.packageValue = mWeiXinResponseBean.packageX
        request.sign = mWeiXinResponseBean.sign
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api!!.sendReq(request)
    }

}

